// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;

import javax.swing.UIManager;

import client.*;
import common.*;

/**
 * This class constructs the UI for a chat client.  It implements the
 * chat interface in order to activate the display() method.
 * Warning: Some of the code here is cloned in ServerConsole 
 *
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Dr Timothy C. Lethbridge  
 * @author Dr Robert Lagani&egrave;re
 * @version July 2000
 */

public class ClientConsole implements ChatIF 
{
  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
  public static  ClientConsole chat;
  public  Object temp;

  //nothig!!!!!
  //yyyyyyyyyyyyyyyy
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
  ChatClient client;
 
  
  
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientConsole(String host, int port) 
  {
   
	  try 
    {
      client= new ChatClient(host, port, this);
    } 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!"
                + " Terminating client.");
      System.exit(1);
    }
  }

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   * @return 
   */
  public void accept(Object message) 
  {
	  this.temp=null;
	  client.handleMessageFromClientUI(message);
	  //return this;
  }

  /**
   * This method overrides the method in the ChatIF interface.  It initial temp 
   * with the message and displays a message onto the screen.
   *
   * @param message The string to be displayed.
   */
  public void display(Object message) 
  {
	  Global.chat.temp=message;
	  System.out.println(">>>> " + message.toString());
	  System.out.println("TEMP ISssssssss  " +  Global.chat.temp.toString());
  }    
 
  /**
   * This method returned the message back to the control 
   * @return message from server
   */
  
  public Object getMsg()
  {   
	   System.out.print("");
	//	Object msg = Global.chat.temp;
	//	Global.chat.temp = null;
	//	  System.out.println("get msg : " );
	   return (this.temp);
  }
      
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of the Client UI.
   *
   * @param args[0] The host to connect to.
   */
  public static void main(String[] args) 
  {
	  try {
          UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
      } catch (Exception e) {
          e.printStackTrace();
      }
    String host = "";
    int port = 0;  //The port number
    
    new EnterIP();

  }
}
//End of ConsoleChat class
